const {
  $util,
} = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uploaddesc: '',
    img: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opts) {
    this.setData(opts, () => {
      this.loadDetail();
    });
  },
  loadDetail() {
    $util.request({
      url: 'entry/wxapp/partywork.index.gettaskdetail',
      method: 'GET',
      data: {
        id: this.data.id,
      },
      success: (res) => {
        res.data.uploadpath && res.data.uploadpath.length && (res.data.img = res.data.uploadpath);
        this.setData(res.data);
        res.data.itemname && wx.setNavigationBarTitle({
          title: res.data.itemname,
        });
      },
    });
  },
  downLoadFile(e) {
    wx.showLoading({
      title: '下载中',
      mask: true,
    });
    wx.downloadFile({
      url: e.currentTarget.dataset.url,
      success: (res) => {
        if (res.statusCode === 200) {
          wx.saveFile({
            tempFilePath: res.tempFilePath,
            success: (result) => {
              console.log(result);
              wx.showModal({
                title: '提示',
                content: '是否直接打开该文件',
                success: (re) => {
                  if (re.confirm) {
                    wx.openDocument({
                      filePath: result.savedFilePath,
                    });
                  }
                },
              });
            },
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
      complete: (res) => {
        wx.hideLoading();
      },
    });
  },
  imgsOnChange(e) {
    this.setData({
      img: e.detail,
    });
  },
  bindInputValue(e) {
    this.setData({
      uploaddesc: e.detail.value,
    })
  },
  submit() {
    if (this.data.img.length === 0) {
      return wx.showToast({
        title: '请上传图片',
        icon: 'none'
      })
    }
    $util.request({
      url: 'entry/wxapp/partywork.index.gettaskdetail',
      method: 'POST',
      data: {
        id: this.data.id,
        uploadpath: this.data.img,
        uploaddesc: this.data.uploaddesc,
      },
      success: (res) => {
        wx.navigateBack();
      },
    });
  },
});